package com.helmlabs.grouper.webapp.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.helmlabs.grouper.accounts.model.Company;
import com.helmlabs.grouper.accounts.model.UserProfile;
import com.helmlabs.grouper.accounts.service.AccountManager;
import com.helmlabs.grouper.accounts.service.AccountManagerFactory;

@Controller
@RequestMapping({"/accounts/create.html","/accounts/edit.html", "/accounts/save.html","/accounts/list.html",
		"/accounts/createCompany.html","/accounts/editCompany.html","/accounts/companyList.html","/accounts/companyRoleXml.html"})
@SessionAttributes("profile")
public class AccountManagementController {
	
	private final AccountManager accountManager = AccountManagerFactory.getAccountManager();
	
	@SuppressWarnings("unchecked")
	@RequestMapping(method=RequestMethod.GET, value="/accounts/edit.html")
	public ModelAndView prepareEditUser(@RequestParam("userid") Long id){
		ModelAndView model = new ModelAndView("accounts/userForm");
		model.addObject("user", accountManager.getUserProfile(id));
		model.addObject("profile", accountManager.getUserProfile(id));
		return model;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(method=RequestMethod.GET, value="/accounts/list.html")
	public ModelAndView prepareList(@RequestParam("page") int page, @RequestParam("size") int size, @RequestParam("sort") String sort, @RequestParam("order") String order){
		ModelAndView model = new ModelAndView("accounts/userList");
		model.addObject("people", accountManager.getUserProfiles(page, size, sort, order));
		return model;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(method=RequestMethod.GET, value="/accounts/create.html")
	public ModelAndView prepareCreate(){
		ModelAndView model = new ModelAndView("accounts/userForm");
		model.addObject("profile", new UserProfile());
		return model;
	}
	
	@RequestMapping(method = RequestMethod.POST, value="/accounts/save.html")
	public String processAccountForm( @ModelAttribute("profile") UserProfile userProfile, BindingResult result, SessionStatus status){
		if (result.hasErrors()){
			return "accounts/userForm";
		}
		accountManager.saveUserProfile(userProfile);
		return "redirect:/accounts/list.html?page=0&size=25&sort=lastName&order=desc";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(method=RequestMethod.GET, value="/accounts/createCompany.html")
	public ModelAndView prepareCreateCompany(){
		ModelAndView model = new ModelAndView("accounts/companyForm");
		model.addObject("company", new Company());
		return model;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(method=RequestMethod.GET, value="/accounts/editCompany.html")
	public ModelAndView prepareEditCompany(@RequestParam("companyid") Long companyid){
		ModelAndView model = new ModelAndView("accounts/companyForm");
		model.addObject("company", accountManager.getCompanyById(companyid));
		model.addObject("employees", accountManager.getEmployeesByCompanyId(companyid));
		return model;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(method=RequestMethod.GET, value="/accounts/companyList.html")
	public ModelAndView prepareCompanyList(@RequestParam("page") int page, @RequestParam("size") int size, @RequestParam("sort") String sort, @RequestParam("order") String order){
		ModelAndView model = new ModelAndView("accounts/companyList");
		model.addObject("companies", accountManager.getCompanies(page, size, sort, order));
		return model;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(method=RequestMethod.GET, value="/accounts/companyRoleXml.html")
	public ModelAndView prepareCompanyRoleList(@RequestParam("parent") String parent){
		ModelAndView model = new ModelAndView("accounts/companyRoleXml");
		Long parentid = null;
		if (parent!=null && !"".equals(parent)){
			parentid = new Long(parent);
		}
		model.addObject("roles", accountManager.getCompanyRolesByParent(parentid));
		return model;
	}


	


}
